namespace WebinatorTest.BaseTests
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using CassiniDev;
    using NUnit.Framework;
    using Webinator;

    [TestFixture]
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
    public class BaseTest
    {
        private readonly Config.AvailableBrowsers _executionBrowser;

        private readonly Config.AvailableFrameworks _executionFramework;

        protected CassiniDevServer HostServer;

        protected string OutputPath;

        protected IWebManager Web;

        protected BaseTest(Config.AvailableBrowsers browser, Config.AvailableFrameworks framework)
        {
            _executionBrowser = browser;
            _executionFramework = framework;
        }

        /// <summary>
        /// Runs before each TestFixture.
        /// </summary>
        [TestFixtureSetUp]
        public virtual void TestFixtureSetUp()
        {
            // Using CassiniDev4-lib.dll
            HostServer = new CassiniDevServer();
            HostServer.StartServer(AppDomain.CurrentDomain.BaseDirectory + "//TestFiles", 8181, "TestFiles", "localhost");

            OutputPath = string.Format(@"TestResults\{0}", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
            var config = new Config
            {
                LogScreenshots = true,
                LogLevel = Config.AvailableLogLevels.Verbose,
                BaseUrl = "http://localhost:8181/TestFiles/Home.html",
                Browser = _executionBrowser,
                Framework = _executionFramework,
                LogDirectory = OutputPath,
            };
            Web = WebManagerFactory.CreateInstance(config);
        }

        /// <summary>
        /// Runs after each TestFixture.
        /// </summary>
        [TestFixtureTearDown]
        public virtual void TestFixtureTearDown()
        {
            Web.CloseBrowser();
            HostServer.StopServer();
        }

        protected void Open(string htmlPageName)
        {
            string url = HostServer.NormalizeUrl(htmlPageName);
            Web.GoToUrl(url);
        }
    }
}